{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"user\":{\"id\":\"JW80368\"},\"token\":{\"expiry\":14400.0},\"permissions\":[\"CONSULTANT\",\"MULTI_SIMULATION\",\"PROD_ALPHAS\",\"REFERRAL\",\"SUPER_ALPHA\",\"VISUALIZATION\",\"WORKDAY\"]}'\n"
     ]
    }
   ],
   "source": [
    "from machine_lib import *\n",
    "s = login()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'{\"user\":{\"id\":\"JW80368\"},\"token\":{\"expiry\":21600.0},\"permissions\":[\"CONSULTANT\",\"MULTI_SIMULATION\",\"PROD_ALPHAS\",\"REFERRAL\",\"SUPER_ALPHA\",\"VISUALIZATION\",\"WORKDAY\"]}'\n",
      "                   id                                        description  \\\n",
      "0        mdl138_3idpc  Composite of all 3 Factor model based on Point...   \n",
      "1       mdl138_3idpqc  Composite of all Quality 3 Factor model based ...   \n",
      "2        mdl138_4idpc  Composite of all 4 Factor model based on Point...   \n",
      "3       mdl138_4idpqc  Composite of all Quality 4 Factor model based ...   \n",
      "4        mdl138_5idpc  Composite of all 5 Factor model based on Point...   \n",
      "..                ...                                                ...   \n",
      "115  mdl138_vcer_4idp                4 Factor model based on Receivables   \n",
      "116  mdl138_vcer_5idp                5 Factor model based on Receivables   \n",
      "117    mdl138_vd_3idp             3 Factor model based on Cash Dividends   \n",
      "118    mdl138_vd_4idp             4 Factor model based on Cash Dividends   \n",
      "119    mdl138_vd_5idp             5 Factor model based on Cash Dividends   \n",
      "\n",
      "                                               dataset  \\\n",
      "0    {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "1    {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "2    {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "3    {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "4    {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "..                                                 ...   \n",
      "115  {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "116  {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "117  {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "118  {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "119  {'id': 'model138', 'name': 'Stock Selection fr...   \n",
      "\n",
      "                             category  \\\n",
      "0    {'id': 'model', 'name': 'Model'}   \n",
      "1    {'id': 'model', 'name': 'Model'}   \n",
      "2    {'id': 'model', 'name': 'Model'}   \n",
      "3    {'id': 'model', 'name': 'Model'}   \n",
      "4    {'id': 'model', 'name': 'Model'}   \n",
      "..                                ...   \n",
      "115  {'id': 'model', 'name': 'Model'}   \n",
      "116  {'id': 'model', 'name': 'Model'}   \n",
      "117  {'id': 'model', 'name': 'Model'}   \n",
      "118  {'id': 'model', 'name': 'Model'}   \n",
      "119  {'id': 'model', 'name': 'Model'}   \n",
      "\n",
      "                                           subcategory region  delay universe  \\\n",
      "0    {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "1    {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "2    {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "3    {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "4    {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "..                                                 ...    ...    ...      ...   \n",
      "115  {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "116  {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "117  {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "118  {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "119  {'id': 'model-mlai-models', 'name': 'ML/AI Mod...    EUR      1  TOP1200   \n",
      "\n",
      "       type  coverage  userCount  alphaCount  pyramidMultiplier themes  \n",
      "0    VECTOR    0.8796          4           5                2.0     []  \n",
      "1    VECTOR    0.8796          3           3                2.0     []  \n",
      "2    VECTOR    0.8796          5           7                2.0     []  \n",
      "3    VECTOR    0.8795          8          13                2.0     []  \n",
      "4    VECTOR    0.8796          3           3                2.0     []  \n",
      "..      ...       ...        ...         ...                ...    ...  \n",
      "115  VECTOR    0.8697          0           0                2.0     []  \n",
      "116  VECTOR    0.8667          0           0                2.0     []  \n",
      "117  VECTOR    0.6117          0           0                2.0     []  \n",
      "118  VECTOR    0.6099          0           0                2.0     []  \n",
      "119  VECTOR    0.6021          0           0                2.0     []  \n",
      "\n",
      "[120 rows x 14 columns]\n"
     ]
    }
   ],
   "source": [
    "s = login()\n",
    "region = \"EUR\"  # EUR TWN\n",
    "id = \"model138\"  # \"other423\" analyst48\n",
    "\n",
    "if region == \"AMR\":\n",
    "    uni = \"TOP600\"\n",
    "elif region == \"JPN\":\n",
    "    uni = \"TOP1600\"\n",
    "elif region == \"USA\":\n",
    "    uni = \"TOP3000\"\n",
    "elif region == \"ASI\":\n",
    "    uni = \"MINVOL1M\"\n",
    "elif region == \"KOR\":\n",
    "    uni = \"TOP600\"\n",
    "elif region == \"TWN\":\n",
    "    uni = \"TOP500\"\n",
    "elif region == \"HKG\":\n",
    "    uni = \"TOP800\"\n",
    "elif region == \"GLB\":\n",
    "    uni = \"TOP3000\"\n",
    "elif region == \"CHN\":\n",
    "    uni = \"TOP2000U\"\n",
    "elif region == \"EUR\":\n",
    "    uni = \"TOP1200\"\n",
    "datafields = get_datafields(s, dataset_id = id, region=region, universe=uni)\n",
    "print(datafields)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Empty DataFrame\n",
      "Columns: [id, description, dataset, category, subcategory, region, delay, universe, type, coverage, userCount, alphaCount, pyramidMultiplier, themes]\n",
      "Index: []\n"
     ]
    }
   ],
   "source": [
    "datafields = datafields[\n",
    "            (datafields[\"coverage\"] > 0.6)\n",
    "            ]\n",
    "print(datafields)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "pc_fields = process_datafields(datafields, \"matrix\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "pc_fields = process_datafields(datafields, \"vector\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def custom_shuffle(alpha_list, interval):\n",
    "    shuffled_list = []\n",
    "    n = len(alpha_list)\n",
    "    for i in range(interval):\n",
    "        shuffled_list.append(alpha_list[i:n:interval])\n",
    "    flattened_list = [item for sublist in shuffled_list for item in sublist]\n",
    "    return flattened_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EUR_fo_model138\n",
      "len of fo_alpha_list: 32640\n",
      "DataFrame successfully saved.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "operators = arsenal + ts_ops\n",
    "# print(operators)\n",
    "first_order = first_order_factory(pc_fields, operators)\n",
    "\n",
    "name = f\"{region}_fo_{id}\"\n",
    "print(name)\n",
    "init_decay = 5\n",
    "fo_alpha_list = []\n",
    "for alpha in first_order:\n",
    "    fo_alpha_list.append((alpha, init_decay))\n",
    "\n",
    "print(f\"len of fo_alpha_list: {len(fo_alpha_list)}\")\n",
    "\n",
    "# shuffled_list = custom_shuffle(fo_alpha_list, 129)\n",
    "\n",
    "df_fo_alpha = pd.DataFrame(fo_alpha_list, columns=[\"alpha\", \"decay\"])\n",
    "output_dir = \"./fo_alpha_list/\"\n",
    "os.makedirs(output_dir, exist_ok=True)\n",
    "try:\n",
    "    df_fo_alpha.to_csv(f\"./fo_alpha_list/{name}.csv\", index=False)\n",
    "    print(f\"DataFrame successfully saved.\")\n",
    "except Exception as e:\n",
    "    print(f\"Error saving DataFrame to CSV: {e}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
